'use strict'

const {
  factSchema,
  findFacts,
  findRandomFact,
  insertFact,
  verifyFact,
  deleteFact
} = require('../models/fact')

module.exports = async function (fastify, opts) {
  // Define JSON schema for route validation
  const getFactsSchema = {
    schema: {
      querystring: {
        type: 'object',
        properties: {
          category: { type: 'string' },
          verified: { type: 'boolean' },
          limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
          skip: { type: 'integer', minimum: 0, default: 0 }
        }
      },
      response: {
        200: {
          type: 'array',
          items: factSchema
        }
      }
    }
  }

  const getRandomFactSchema = {
    schema: {
      querystring: {
        type: 'object',
        properties: {
          category: { type: 'string' }
        }
      },
      response: {
        200: factSchema,
        404: {
          type: 'object',
          properties: {
            message: { type: 'string' }
          }
        }
      }
    }
  }

  const createFactSchema = {
    schema: {
      body: {
        type: 'object',
        properties: {
          text: { type: 'string' },
          category: { type: 'string' },
          source: { type: 'string' },
          verified: { type: 'boolean' }
        },
        required: ['text', 'category']
      },
      response: {
        201: factSchema
      }
    }
  }

  const verifyFactSchema = {
    schema: {
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
        },
        required: ['id']
      },
      response: {
        200: factSchema,
        404: {
          type: 'object',
          properties: {
            message: { type: 'string' }
          }
        }
      }
    }
  }

  const deleteFactSchema = {
    schema: {
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
        },
        required: ['id']
      },
      response: {
        200: {
          type: 'object',
          properties: {
            message: { type: 'string' }
          }
        },
        404: {
          type: 'object',
          properties: {
            message: { type: 'string' }
          }
        }
      }
    }
  }

  // Routes
  fastify.get('/api/facts', getFactsSchema, async function (request, reply) {
    const { category, verified, limit, skip } = request.query
    
    // Build query based on parameters
    const query = {}
    if (category) query.category = category
    if (verified !== undefined) query.verified = verified
    
    const facts = await findFacts(fastify, query, limit, skip)
    return facts
  })

  fastify.get('/api/facts/random', getRandomFactSchema, async function (request, reply) {
    const { category } = request.query
    const fact = await findRandomFact(fastify, category)
    
    if (!fact) {
      return reply.code(404).send({ message: 'No facts found' })
    }
    
    return fact
  })

  fastify.post('/api/facts', createFactSchema, async function (request, reply) {
    const newFact = await insertFact(fastify, request.body)
    reply.code(201)
    return newFact
  })

  fastify.post('/api/facts/:id/verify', verifyFactSchema, async function (request, reply) {
    const { id } = request.params
    const fact = await verifyFact(fastify, id)
    
    if (!fact) {
      return reply.code(404).send({ message: 'Fact not found' })
    }
    
    return fact
  })

  fastify.delete('/api/facts/:id', deleteFactSchema, async function (request, reply) {
    const { id } = request.params
    const result = await deleteFact(fastify, id)
    
    if (result.deletedCount === 0) {
      return reply.code(404).send({ message: 'Fact not found' })
    }
    
    return { message: 'Fact deleted successfully' }
  })
} 